/**
 * 
 */
package data;

import java.io.File;
import java.util.ArrayList;

import exceptions.IllegalPatternException;
import exceptions.NoSplitSignException;
import exceptions.TooManyFilesException;
import exceptions.VariableDuplicationException;

/**
 * This class provides static methods for general use.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class StaticMethods {

	/**
	 * Lists all files in the given directory.
	 * 
	 * @param dir
	 *            The directory to search in
	 * @param subDir
	 *            {@code true}, if subdirectories should be considered
	 * @return a list of files in the given directory
	 * @throws TooManyFilesException
	 *             exception if number of files is greater than
	 *             Integer.MAX_VALUE.
	 */
	public static ArrayList<File> getFilesInDir(File dir, boolean subDir)
			throws TooManyFilesException {
		ArrayList<File> list = new ArrayList<File>();
		File[] temp = dir.listFiles();
		for (int i = 0; i < temp.length; i++) {
			if (list.size() >= Integer.MAX_VALUE) {
				throw new TooManyFilesException();
			}
			if (temp[i].isFile()) {
				list.add(temp[i]);
			} else {
				if (subDir) {
					list.addAll(getFilesInDir(temp[i], true));
				}
			}
		}
		return list;
	}

	/**
	 * 
	 * @param text
	 *            the text to search in
	 * @param sign
	 *            the sign to search
	 * @return array of positions of the sign in the text
	 */
	public static int[] charInText(String text, char sign) {
		int count = 0;
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) == sign) {
				count++;
			}
		}
		int[] back = new int[count];
		count = 0;
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) == sign) {
				back[count] = i;
				count++;
			}
		}
		return back;
	}
	
	/**
	 * 
	 * @param strings list of strings
	 * @return {@code true} if there are duplicated strings in the list of strings
	 */
	public static boolean checkDuplicates(String[][] strings){
		for(int i=0;i<strings.length-1;i++){
			for(int j=i;j<strings.length;j++){
				if(strings[i][0].equals(strings[j])){
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @param pattern the pattern to parse the text
	 * @param text the text to parse
	 * @return an array of variables an values from the text with the given pattern.
	 * @throws IllegalPatternException {@link IllegalPatternException}
	 * @throws NoSplitSignException {@link NoSplitSignException}
	 * @throws VariableDuplicationException {@link VariableDuplicationException}
	 */
	public static String[][] parseForward(String pattern, String text) throws IllegalPatternException, NoSplitSignException, VariableDuplicationException {
		int[] pos=charInText(pattern, (char)37);
		if(pos.length%2==1||pos.length==0){
			throw new IllegalPatternException();
		}
		String[][] variables=new String[pos.length/2][2];
		for(int i=0;i<pos.length/2;i++){
			variables[i][0]=pattern.substring(pos[2*i], pos[2*i+1]+1);
			//System.out.println("Variable "+(i+1)+" :"+variables[i][0]);
		}
		if(checkDuplicates(variables)){
			throw new VariableDuplicationException();
		}
		String[] splitSign=new String[pos.length/2-1];
		for(int i=0;i<pos.length/2-1;i++){
			int firstIndex=2*i+1;
			int secondIndex=2*i+2;
			if(pos[firstIndex]+1==pos[secondIndex]){
				throw new NoSplitSignException();
			}
			splitSign[i]=pattern.substring(pos[firstIndex]+1, pos[secondIndex]);
			//System.out.println("Splitter "+(i+1)+" :|"+splitSign[i]+"|");
		}
		String temp=text;
		for(int i=0;i<splitSign.length;i++){
			variables[i][1]=temp.substring(0, temp.indexOf(splitSign[i]));
			temp=temp.replaceFirst(variables[i][1]+splitSign[i], "");
		}
		variables[variables.length-1][1]=temp;
		return variables;
	}
	
	/**
	 * @param pattern the pattern to parse the text
	 * @param text the text to parse
	 * @return an array of variables an values from the text with the given pattern.
	 * @throws IllegalPatternException {@link IllegalPatternException}
	 * @throws NoSplitSignException {@link NoSplitSignException}
	 * @throws VariableDuplicationException {@link VariableDuplicationException}
	 */
	public static String[][] parseBackward(String pattern, String text) throws IllegalPatternException, NoSplitSignException, VariableDuplicationException {
		int[] pos=charInText(pattern, (char)37);
		if(pos.length%2==1||pos.length==0){
			throw new IllegalPatternException();
		}
		String[][] variables=new String[pos.length/2][2];
		for(int i=0;i<pos.length/2;i++){
			variables[i][0]=pattern.substring(pos[2*i], pos[2*i+1]+1);
		}
		if(checkDuplicates(variables)){
			throw new VariableDuplicationException();
		}
		String[] splitSign=new String[pos.length/2-1];
		for(int i=0;i<pos.length/2-1;i++){
			int firstIndex=2*i+1;
			int secondIndex=2*i+2;
			if(pos[firstIndex]+1==pos[secondIndex]){
				throw new NoSplitSignException();
			}
			splitSign[i]=pattern.substring(pos[firstIndex]+1, pos[secondIndex]);
			//System.out.println("Splitter "+(i+1)+" :|"+splitSign[i]+"|");
		}
		String temp=text;
		for(int i=0;i<splitSign.length;i++){
			variables[i][1]=temp.substring(0, temp.indexOf(splitSign[i]));
			temp=temp.replaceFirst(variables[i][1]+splitSign[i], "");
		}
		variables[variables.length-1][1]=temp;
		return variables;
	}

}
